package com.jl15988.simpletools.appcore.controller;

import com.jl15988.simpletools.appcore.dto.CategoryDTO;
import com.jl15988.simpletools.appcore.dto.CategoryRequestDTO;
import com.jl15988.simpletools.appcore.service.CategoryService;
import com.jl15988.simpletools.common.annotation.Log;
import com.jl15988.simpletools.common.constant.OperationType;
import com.jl15988.simpletools.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类控制器
 *
 * @author jl15988
 */
@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类
     *
     * @return 分类列表
     */
    @GetMapping
    public Result<List<CategoryDTO>> getAllCategories() {
        return Result.success(categoryService.getAllCategories());
    }

    /**
     * 获取分类详情
     *
     * @param id 分类ID
     * @return 分类详情
     */
    @GetMapping("/{id}")
    public Result<CategoryDTO> getCategoryDetail(@PathVariable Long id) {
        return Result.success(categoryService.getCategoryDetail(id));
    }

    /**
     * 创建分类
     *
     * @param request 分类请求DTO
     * @param icon 分类图标
     * @return 分类ID
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "创建分类", operationType = OperationType.CREATE)
    public Result<Long> createCategory(@Valid CategoryRequestDTO request,
                                      @RequestParam(value = "icon", required = false) MultipartFile icon) {
        return Result.success(categoryService.createCategory(request, icon));
    }

    /**
     * 更新分类
     *
     * @param request 分类请求DTO
     * @param icon 分类图标
     * @return 是否成功
     */
    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "更新分类", operationType = OperationType.UPDATE)
    public Result<Boolean> updateCategory(@Valid CategoryRequestDTO request,
                                        @RequestParam(value = "icon", required = false) MultipartFile icon) {
        return Result.success(categoryService.updateCategory(request, icon));
    }

    /**
     * 删除分类
     *
     * @param id 分类ID
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "删除分类", operationType = OperationType.DELETE)
    public Result<Boolean> deleteCategory(@PathVariable Long id) {
        return Result.success(categoryService.deleteCategory(id));
    }

    /**
     * 根据应用ID获取分类列表
     *
     * @param appId 应用ID
     * @return 分类列表
     */
    @GetMapping("/app/{appId}")
    public Result<List<CategoryDTO>> getCategoriesByAppId(@PathVariable Long appId) {
        return Result.success(categoryService.getCategoriesByAppId(appId));
    }
} 